package com.java2007.xiazhaodong.hotel.dao.impl;


import com.java2007.xiazhaodong.hotel.dao.FoodTypeDao;
import com.java2007.xiazhaodong.hotel.entity.FoodType;
import com.java2007.xiazhaodong.hotel.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2021/1/19 17:27
 * @Version 1.0
 */
public class FoodTypeDaoImpl implements FoodTypeDao {
    private QueryRunner queryRunner=new QueryRunner();
    //增加
    @Override
    public void save(FoodType foodType) {
        String sql ="insert into foodType(typeName) values (?)";
        try {
            queryRunner.update(DbUtils.getConnection(),sql,foodType.getTypeName());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("插入时出现错误");
            throw new RuntimeException(e);

        }


    }
    //更新
    @Override
    public void update(FoodType foodType) {
        String sql ="update foodType set typeName=? where id=?";
        try {
            queryRunner.update(DbUtils.getConnection(),sql,foodType.getTypeName(),foodType.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改时出现错误");
            throw new RuntimeException(e);

        }

    }
    //删除
    @Override
    public void delete(int id) {
        String sql ="delete from foodType where id=?";
        try {
            queryRunner.update(DbUtils.getConnection(),sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除时出现错误");
            throw new RuntimeException(e);

        }


    }
    //根据id查询菜系
    @Override
    public FoodType findById(int id) {

        String sql ="select id,typeName from foodType where id =? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(FoodType.class) ,id);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询时出现异常");
            throw new RuntimeException(e);

        }


    }
    //查询所有菜谱
    @Override
    public List<FoodType> getAll() {
        String sql ="select id,typeName from foodType";
        try {
            return  queryRunner.query(DbUtils.getConnection(),sql,new BeanListHandler<>(FoodType.class));
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询时出现异常");
            throw new RuntimeException(e);
        }


    }
    //按照菜系名称查询所有菜谱
    @Override
    public List<FoodType> getAll(String typeName) {
        String sql="select id,typeName where typeName like ?";
        try {
            //使用模糊查询
            return queryRunner.query(DbUtils.getConnection(),sql,new BeanListHandler<>(FoodType.class),"%"+ typeName+"%");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询时出现异常");
            throw new RuntimeException(e);
        }

    }
}
